##################################################################
# /* Author: Gautam Nair and Nicholas Sambanis */
# /* Violence Exposure and Ethnic Identification: Evidence from Kashmir */
# /* Table 19:  Average Treatment Effects: Full Randomization */

##################################################################

# change working directory here
# setwd("")

##################################################################
# loading packages
##################################################################
library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(dplyr)
library(scales)

##################################################################

rm(list=ls())

library(stargazer)

data.working <- read.csv("nei_kashmir_replication_dataset.csv")

dep.var <- c("identity_rank_indian_rev", 
"identity_choice_indian_both", 
"kashmir_status_india",  
"ind_pak_prefer_ind",
"bonus_donation_india", 
"ic_index_id_no_ind_pak",
"ic_index_id_ind_pak",
"protest_peaceful_endorse",
"protest_violent_endorse"
)

data.working$group_temp_2 <- as.numeric(data.working$group_rand1_rand2==2)
data.working$group_temp_3 <- as.numeric(data.working$group_rand1_rand2==3)
data.working$group_temp_4 <- as.numeric(data.working$group_rand1_rand2==4)
data.working$group_temp_5 <- as.numeric(data.working$group_rand1_rand2==5)
data.working$group_temp_6 <- as.numeric(data.working$group_rand1_rand2==6)
data.working$group_temp_7 <- as.numeric(data.working$group_rand1_rand2==7)
data.working$group_temp_8 <- as.numeric(data.working$group_rand1_rand2==8)


ind.var <- c( "group_temp_2" ,  
"group_temp_3", 
"group_temp_4", 
"group_temp_5",
"group_temp_6",
"group_temp_7",
"group_temp_8"
)


reg.models <-vector("list", length(dep.var)) 
se.models <-vector("list", length(dep.var))
for(i in 1:length(dep.var)){
	temp.data <- data.working[,c(dep.var[i],ind.var)]
	temp.data <- na.omit(temp.data)
	dep.var[i]
	my.formula <-paste(dep.var[i],'~', paste(ind.var, collapse= ' + '))
	temp.model <- lm(my.formula, data=temp.data)
	temp.se <- coeftest(temp.model, vcov=vcovHC(temp.model,type="HC2"))[,2]
	se.models[[i]] <- temp.se
	reg.models[[i]] <- temp.model
}

# stargazer customization

varlabels <-       c("Violence Exposure", "Economic Growth", "Economic Growth + Violence", "Integrative Institutions", "Integrative Institutions + Violence", "Common Geography", "Common Geography + Violence", "Constant (Control Group Mean)")
spectitle <- c("Average Treatment Effects: All Groups")
output.file <- c("tf_t_19_ate_violence_status_geo_int_inst")
temptype <- c("latex", "text")
tempext <- c(".tex", ".txt")

for(q in 1:length(temptype)){
	temp.output <- paste(output.file, tempext[q], sep="") 
 stargazer(reg.models,se=se.models,
         title= spectitle,
          out=temp.output,
          no.space=TRUE, 
          model.numbers= TRUE,
         notes.append=FALSE,
          align=FALSE,
         covariate.labels=varlabels,
          header= FALSE,
          font.size="small",
          model.names=FALSE,
          digits=2,
         star.char=c("**", "***"),
         star.cutoffs=c(0.05, 0.01),
          omit.stat = c("rsq", "f","adj.rsq"),
          column.labels  = NULL,
          dep.var.caption  = "",
          dep.var.labels.include=FALSE,
         df=FALSE,
         omit.table.layout="n",
         type= temptype[q]
) 
}








